package com.chocomania;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;

import com.dao.ChocomaniaSqlLiteHelper;

public class Ramdom  extends Activity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		ChocomaniaSqlLiteHelper chocoDb = new ChocomaniaSqlLiteHelper(this);
		SQLiteDatabase db = chocoDb.getWritableDatabase();
		
		//TRAIGO TODAS LAS RECETAS
        String[] columns = {"_id"};
        Cursor c = db.query("receta",columns,null, null,null, null, null, null);
		startManagingCursor(c);
		
		//ARMO EL NUMERO RAMDOM DE RECETA
		Random myRandom = new Random();
		int limite = c.getCount();
		int num = myRandom.nextInt(limite+1);
		
		//BUSCO LA RECETA RAMDOM
		String id = "1";
		int count=0;
		if (c != null) {
	        while (c.moveToNext()){
	        	if(num==count){
	        		id = c.getString(0);
	        	}
	        	count++;
	        }
		}
		
		//CARGO EL DETALLE DE RECETA
    	Intent i = new Intent(Ramdom.this,DetalleDeReceta.class);
    	i.putExtra("idReceta",id); //envio la receta
    	i.putExtra("random","true"); //envio la receta
    	startActivity(i);
    	
	};
	
	@Override
	public boolean onKeyDown (int keyCode, KeyEvent event){
		if(keyCode == KeyEvent.KEYCODE_BACK){ 
			Intent i = new Intent(Ramdom.this,Inicio.class);
	    	startActivity(i);
		} 
		return false;
	}
}
